package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.CommonUtil;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public int saveOrder(String userId, Integer businessId, Integer daId, String orderTotal) {
        int orderId = 0;
        try {
            DBUtil.beginTransaction();
            CartDaoImpl cartDao = new CartDaoImpl();
            Cart cart = new Cart();
            cart.setUserId(userId);
            cart.setBusinessId(businessId);
            List<Cart> list  = cartDao.listCart(cart);

            OrdersDaoImpl ordersDao = new OrdersDaoImpl();
            Orders orders = new Orders();
            orders.setUserId(userId);
            orders.setBusinessId(businessId);
            String date = CommonUtil.getCurrentDate();
            orders.setOrderDate(date);
            orders.setOrderTotal(orderTotal);
            orders.setDaId(daId);
            orders.setOrderState(0);
            orderId = ordersDao.saveOrder(orders);

            List<Orderdetailet> list1 = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Cart cart1 = list.get(i);
                Orderdetailet orderdetailet = new Orderdetailet();
                Integer foodId = cart1.getFoodId();
                Integer quantity = cart1.getQuantity();
                orderdetailet.setFoodId(foodId);
                orderdetailet.setQuantity(quantity);
                orderdetailet.setOrderId(orderId);
                list1.add(orderdetailet);
            }
            OrderdetailetDaoImpl orderdetailetDao = new OrderdetailetDaoImpl();
            orderdetailetDao.save(list1);
            cartDao.removeCart(cart);

            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
                DBUtil.close();
            }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        OrdersDaoImpl ordersDao = new OrdersDaoImpl();
        try {
            orders = ordersDao.getOrdersById(orderId);

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {

        OrdersDaoImpl ordersDao = new OrdersDaoImpl();
        OrderdetailetDaoImpl orderdetailetDao = new OrderdetailetDaoImpl();
        List<Orders> list2 = new ArrayList<>();
        try {
            List<Orders> list = ordersDao.listOrdersByUserId(userId);
            for (int i = 0;i<list.size();i++){
                Orders orders = list.get(i);
                List<Orderdetailet> list1 = orderdetailetDao.save2(orders);
                orders.setList(list1);
                list2.add(orders);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list2;
    }
}
